package com.edu.chongdian.viewmodels;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import androidx.appcompat.app.AppCompatActivity;

import com.edu.chongdian.BalanceAdapter;
import com.edu.chongdian.R;

public class ChongzhiActivity extends AppCompatActivity {
    private int[] mBalanceOptions = {10, 30, 50, 100, 200, 500};
    BalanceAdapter adapter;
    RadioButton button_one;
    RadioButton button_two;
    RadioButton button_three;
    Button button;
    LinearLayout alayout;
    LinearLayout blayout;
    LinearLayout clayout;
    LinearLayout dlayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chongzhi);
        button_one=findViewById(R.id.one);
        button_two=findViewById(R.id.two);
        button_three=findViewById(R.id.three);
        alayout=findViewById(R.id.a_layout);
        blayout=findViewById(R.id.b_layout);
        clayout=findViewById(R.id.c_layout);
        dlayout=findViewById(R.id.d_layout);
        button=findViewById(R.id.chongzhi);
        method();
        show();

    }
    private void method(){
        GridView gridView = findViewById(R.id.gridview);
        adapter = new BalanceAdapter(this, mBalanceOptions);
        gridView.setAdapter(adapter);
        adapter.setSelectedPosition(0);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        adapter.setSelectedPosition(0);
                        show1(mBalanceOptions[0]);
                        break;
                    case 1:
                        adapter.setSelectedPosition(1);
                        show1(mBalanceOptions[1]);
                        break;
                    case 2:
                        adapter.setSelectedPosition(2);
                        show1(mBalanceOptions[2]);
                        break;
                    case 3:
                        adapter.setSelectedPosition(3);
                        show1(mBalanceOptions[3]);
                        break;
                    case 4:
                        adapter.setSelectedPosition(4);
                        show1(mBalanceOptions[4]);
                        break;
                    case 5:
                        adapter.setSelectedPosition(5);
                        show1(mBalanceOptions[5]);
                        break;
                    case 6:
                        adapter.setSelectedPosition(6);
                        show1(mBalanceOptions[6]);
                        break;
                }
            }
        });
    }

    private void show1(int num){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VirtualCurrencyManager virtualCurrencyManager = new VirtualCurrencyManager();
                int userId = 12345;
                int total = virtualCurrencyManager.rechargeVirtualCurrency(userId, num);// 充值100元，假设充值成功并返回true
                Log.e("TAG","充值结果"+total);
                int virtualCurrencyBalance = virtualCurrencyManager.getVirtualCurrencyBalance(userId); // 查询虚拟货币余额，结果应该为1000
                //  Log.e("TAG","充值结果"+virtualCurrencyBalance);
                int consume = virtualCurrencyManager.consumeVirtualCurrency(userId, 30);// 消费30元，假设消费成功并返回true
                virtualCurrencyBalance = virtualCurrencyManager.getVirtualCurrencyBalance(userId); // 查询虚拟货币余额，结果应该为700
                Log.e("TAG","消费结果"+consume);
                int finaltotal=total+consume;
                Log.e("TAG",""+finaltotal);
            }
        });
    }
    private void show(){
    // 创建 OnClickListener 对象
    View.OnClickListener radioButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case  R.id.one:
                    button_one.setChecked(true);
                    button_two.setChecked(false);
                    button_three.setChecked(false);
                    break;
                case R.id.two:
                    button_one.setChecked(false);
                    button_two.setChecked(true);
                    button_three.setChecked(false);
                    break;
                case R.id.three:
                    button_one.setChecked(false);
                    button_two.setChecked(false);
                    button_three.setChecked(true);
                    break;
            }
        }
    };
// 给每个 RadioButton 控件设置统一的 OnClickListener 对象
button_one.setOnClickListener(radioButtonClickListener);
button_two.setOnClickListener(radioButtonClickListener);
button_three.setOnClickListener(radioButtonClickListener);
        blayout.setVisibility(View.GONE);
        clayout.setVisibility(View.GONE);
dlayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        blayout.setVisibility(View.VISIBLE);
        clayout.setVisibility(View.VISIBLE);
        dlayout.setVisibility(View.GONE);
    }
});
    }
}